'''
 Copyright (c) 2012, Peter Angyal <angyalp@gmail.com>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the <organization> nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import unittest

from track.trackpath import TrackPath

class Test(unittest.TestCase):

    def test_parsepathdata(self):
        # Only absolute coordinates and one curve
        pathData1 = "M 282.85714,935.80789 C 351.42858,188.77551 422.85715,-78.57141 422.85715,-78.57141 Z"
        pathCommands1 = [["M", [[282.85714, 935.80789]]], 
                 ["C", [[351.42858, 188.77551], [422.85715, -78.57141], [422.85715, -78.57141]]], 
                 ["Z", []]]
        
        # Complex data
        pathData2 = "m 282.85714,935.80789 c 351.42858,188.77551 422.85715,-78.57141 422.85715,-78.57141 0,0 101.42857,-244.69382 30,-338.97951 -71.42858,-94.28569 -404.28572,-181.8367 -404.28572,-181.8367 0,0 -382.857141,-130.20404 -227.14286,177.34689 C 260,821.31812 367.14285,248.86926 551.42857,511.52227 735.71428,774.17528 195.71429,711.31815 195.71429,711.31815 10.695124,662.93625 136.86452,876.27585 282.85714,935.80789 z"
        pathCommands2 = [["m", [[282.85714,935.80789]]], 
                 ["c", [[351.42858,188.77551], [422.85715,-78.57141], [422.85715,-78.57141]]],
                 ["c", [[0,0], [101.42857,-244.69382], [30,-338.97951]]],
                 ["c", [[-71.42858,-94.28569], [-404.28572,-181.8367], [-404.28572,-181.8367]]],
                 ["c", [[0,0], [-382.857141,-130.20404], [-227.14286,177.34689]]],
                 ["C", [[260,821.31812], [367.14285,248.86926], [551.42857,511.52227]]],
                 ["C", [[735.71428,774.17528], [195.71429,711.31815], [195.71429,711.31815]]],
                 ["C", [[10.695124,662.93625], [136.86452,876.27585], [282.85714,935.80789]]],
                 ["z", []]]
        
        trackPath = TrackPath()
        pathCommands = trackPath._parsePathData(pathData1)
        self.assertEqual(pathCommands, pathCommands1, "Parsed data mismatch")

        pathCommands = trackPath._parsePathData(pathData2)
        self.assertEqual(pathCommands, pathCommands2, "Parsed data mismatch")
        
        # TODO: Test erroneous inputs

    def test_parsefile(self):
        trackPath = TrackPath()
        self.assertEqual(len(trackPath.getSections()), 0, "Path should be empty after creation.")

        validSVGFile = "test-track-1.svg"
        notExistingSVGFile = "some-never-existed-file.svg"
        validPathElemID = "IDTrackPath"
        notExistingPathElemID = "noid"
        # This file has one path element which is invalid, otherwise this is a valid SVG file.
        invalidSVGFile = "test-invalid-path.svg"
        invalidPathElemID = "IDInvalidPath"

        pathData = trackPath._loadPathDataFromSVGFile(notExistingSVGFile, notExistingPathElemID)
        self.assertIsNone(pathData, "Loading path data from not existing file should return None.")
        
        pathData = trackPath._loadPathDataFromSVGFile(validSVGFile, notExistingPathElemID)
        self.assertIsNone(pathData, "Loading not existing path elem should return None.")

        pathData = trackPath._loadPathDataFromSVGFile(validSVGFile, validPathElemID)
        self.assertIsNotNone(pathData, "Loading valid path elem definition should not return None.")

        trackPath = TrackPath.parseFromSVGFile(notExistingSVGFile, notExistingPathElemID)
        self.assertIsNone(trackPath, "Parsing not existing path elem should return None.")
        
        trackPath = TrackPath.parseFromSVGFile(validSVGFile, notExistingPathElemID)
        self.assertIsNone(trackPath, "Parsing not existing file should return None.")

        #trackPath = TrackPath.parseFromSVGFile(invalidSVGFile, invalidPathElemID)
        #self.assertIsNone(trackPath, "Parsing a file containing a path with invalid data should return None")

        #trackPath = TrackPath.parseFromSVGFile(validSVGFile, validPathElemID)
        #self.assertIsNotNone(trackPath, "Parsing a valid file should return track path not None")


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()